package com.sgu6.sgu6_machine.adapter;

import android.content.Context;
import android.graphics.Typeface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.sgu6.sgu6_machine.R;
import com.sgu6.sgu6_machine.dto.GoodsSimpleDto;
import com.sgu6.sgu6_machine.util.FileUtils;
import com.sgu6.sgu6_machine.util.ImageDownloadUtil;
import com.sgu6.sgu6_machine.util.OssUtil;

import java.io.File;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class RiceAdapter extends RecyclerView.Adapter implements View.OnClickListener {

    private static String TAG = RiceAdapter.class.getCanonicalName();
    private List<GoodsSimpleDto> goods;
    private Context mContext;

    public RiceAdapter(Context mContext, List<GoodsSimpleDto> goods) {
        this.goods = goods;
        this.mContext = mContext;
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(mContext).inflate(R.layout.item_fragment_select, viewGroup, false);
        v.setOnClickListener(this);
        return new Holder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        viewHolder.itemView.setTag(i);
        if (viewHolder instanceof Holder) {
            if (goods.get(i).getPhoto().size() > 0) {
                String[] names = goods.get(i).getPhoto().get(0).getImgUrl().split("/");
                File file = FileUtils.getLocalImageByName(names[names.length - 1]);
                RequestOptions options = new RequestOptions().diskCacheStrategy(DiskCacheStrategy.NONE);
                if (file.exists()) {
                    Glide.with(mContext).load(file).apply(options).into(((Holder) viewHolder).ivRiceType);
                } else {
                    String key = goods.get(i).getPhoto().get(0).getImgUrl();
                    String url = OssUtil.getInstance(mContext).getURL((key.startsWith("/") ? key.substring(1) : key));

                    ImageDownloadUtil.getInstance().download(url, "/sgu6/images/", names[names.length - 1], new ImageDownloadUtil.OnDownloadListener() {
                        @Override
                        public void onDownloadSuccess() {
                            Log.i(TAG, "onDownloadSuccess: ");
                            ((Holder) viewHolder).ivRiceType.post(new Runnable() {
                                @Override
                                public void run() {
                                    Glide.with(mContext).load(url).apply(options).into(((Holder) viewHolder).ivRiceType);
                                }
                            });
                        }

                        @Override
                        public void onDownloading(int progress) {
                            Log.i(TAG, "onDownloading: " + progress);
                        }

                        @Override
                        public void onDownloadFailed(String url) {
                            Log.i(TAG, "onDownloadFailed: ");
                            try {
                                File file = FileUtils.getLocalImageByName(names[names.length - 1]);
                                file.delete();
                            } catch (Exception e) {
                                Log.i(TAG, "onDownloadFailed: " + e.getLocalizedMessage());
                            }
                        }
                    });
                }
            }
        }
    }

    @Override
    public int getItemCount() {
        return goods.size();
    }

    @Override
    public void onClick(View v) {
        listener.onItemClick((int) v.getTag());
    }


    static class Holder extends RecyclerView.ViewHolder {
        @BindView(R.id.ivRiceType)
        ImageView ivRiceType;

        Holder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    private OnItemClickListener listener;

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    public void setOnClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }
}
